package cn.com.kblog.util;


import java.io.File;
import java.io.StringWriter;
import java.util.Map;

import freemarker.template.Configuration;
import freemarker.template.Template;

/**
 * FreeMarker模板操作类
 * @author KK.Luo
 * Email luokai8@vip.qq.com
 * @date 2013-12-2下午1:36:58
 */
public class FreeMarkerTemplate {
	private String filename;
	private String dir;
	/**
	 * @param dir 模板文件所在的目录
	 * @param filename 模板文件名
	 */
	public FreeMarkerTemplate(String dir,String filename) {
		super();
		this.filename = filename;
		this.dir = dir;
	}
	
	
	/**
	 * 获得HTML
	 * @param rootMap 设置FreeMarker的参数
	 * @return HTML字符窜
	 */
	public String getHtml(Map<String, Object> params){
		//取得freemarker配置实例  
		Configuration configuration = new Configuration();  
		Template template = null;
		StringWriter writer = null;
		//设置模版所在文件夹  
		try {
			//取得我们模版文件  
			configuration.setDirectoryForTemplateLoading(new File(dir));
			template = configuration.getTemplate(filename);  
			writer = new StringWriter();  
			template.process(params, writer); 
		}  catch (Exception e) {
			e.printStackTrace();
		}
		 
        return writer.toString();
	}
	
	
	
}
